package com.monochromebytes.mononet.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import org.newdawn.slick.SlickException;

public class Starter {
    /**
     * Entry point to the test
     * 
     * @param argv
     *            The arguments to pass into the test
     * @throws SlickException
     * @throws IOException
     */
    public static void main(String[] argv) throws SlickException, IOException {
        if (argv.length == 2 || argv.length == 4) {
            InetAddress ip = InetAddress.getByName(argv[0]);
            int port = Integer.parseInt(argv[1]);
            InetSocketAddress localAddress = new InetSocketAddress(ip, port);

            if (argv.length == 2) {
                System.out.println("Starting server bound to (" + localAddress + ").");
                try {
                    new Server(localAddress);
                } catch (IOException e) {
                    System.err.println("Could not start Server: " + e.getMessage() + ".");
                }
            }
            else {
                ip = InetAddress.getByName(argv[2]);
                port = Integer.parseInt(argv[3]);
                InetSocketAddress serverAddress = new InetSocketAddress(ip, port);

                System.out.println("Starting client bound to (" + localAddress
                        + ") and connected to server (" + serverAddress + ").");
                try {
                    new Client(localAddress, serverAddress);
                } catch (IOException e) {
                    System.err.println("Could not start Client: " + e.getMessage() + ".");
                }
            }
        }
        else {
            String help = "Invalid arguments passed.\n\n"
                    + "To start a server type this:\n"
                    + "framework-test.sh/exe <your-ip> <your-port>\n"
                    + "\n"
                    + "To start a client type this:\n"
                    + "framework-test.sh/exe <your-ip> <your-port> <server-ip> <server-port>\n";
            System.out.println(help);
        }

    }
}
